<title>全端复杂组合</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>用户</cite></a>
        <a><cite>资源管理</cite></a>
    </div>
</div>

<style>
    /* 这段样式只是用于演示 */
    #LAY-component-grid-all .layui-card-body {
        display: flex;
        /*justify-content: center;*/
        flex-direction: column;
        /*text-align: center;*/
        /*height: 200px;*/
    }

    .layui-card-header {
        padding: 5px;
    }

    .layui-card-header .layui-icon {
        right: 7px !important;
    }
</style>

<div class="layui-fluid" id="LAY-component-grid-all">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-xs6 layui-col-sm4 layui-col-md2">
            <div class="layui-card">
                <div class="layui-card-header">
                    <div class="layui-row">
                        <input type="text" id="tree_name_search" name="name" autocomplete="off" placeholder="请输入名称或编码"
                               class="layui-input layui-col-md12">
                    </div>
                </div>
                <div class="layui-card-body">
                    <div id="resource_tree" style="height: 500px;"></div>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6 layui-col-sm8 layui-col-md10">
            <div class="layui-card">
                <div class="layui-form layui-card-header layui-form-pane" lay-filter="layadmin-userfront-formlist">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" placeholder="请输入名称或编码" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit
                                    lay-filter="LAY-user-front-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                    </div>

                    <table id="LAY-resource-tb" lay-filter="LAY-resource-tb"></table>
                    <script type="text/html" id="table-resource-list">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                                class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                                class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use('resource/list', layui.factory('resource/list')).use(['admin', 'tree', 'util', 'table', 'form'], function () {
        let $ = layui.$
            , admin = layui.admin
            , table = layui.table
            , element = layui.element
            , view = layui.view
            , treeUtil = layui.tree
            , layer = layui.layer
            , form = layui.form;

        // 设置高度自适应
        let bodyHeight = document.getElementById("LAY_app_body").clientHeight;
        $(".layui-card-body").height(bodyHeight - 150);

        _initTreePanel(treeUtil, admin, view);
        form.render(null, 'layadmin-userfront-formlist');

        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-resource-tb', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('LAY-user-manage');
                        layer.msg('已删除');
                    });
                });
            }
            , add: function () {
                admin.popup({
                    title: '添加资源'
                    , area: ['600px', '650px']
                    , id: 'LAY-popup-resource-add'
                    , shadeClose: false
                    , success: function (layero, index) {
                        view(this.id).render('user/resource/form').done(function () {
                            form.render(null, 'layuiadmin-form-resource');

                            //监听提交
                            form.on('submit(LAY-resource-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                admin.req({
                                    url: '/api/resources/save',
                                    data: JSON.stringify(field),
                                    type: 'post',
                                    dataType: 'json',
                                    contentType: "application/json",
                                    success: function (res) {
                                        layer.msg('操作成功！', {
                                            offset: '15px',
                                            icon: 1,
                                            time: 1000
                                        });
                                    },
                                    error: function (res) {
                                        view.error('操作失败');
                                    }
                                });
                                layui.table.reload('LAY-resource-tb'); //重载表格
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 回车搜索
        $("#tree_name_search").on("keydown",function () {
            let name = document.getElementById('tree_name_search').value
            dispatchForCode(event, name)
        });

        let dispatchForCode = function(event, name) {
            var code;

            if (event.key !== undefined) {
                code = event.key;
            } else if (event.keyIdentifier !== undefined) {
                code = event.keyIdentifier;
            } else if (event.keyCode !== undefined) {
                code = event.keyCode;
            }
            if (code === 'Enter' || code === 13) {
                _initTreePanel(treeUtil, admin, view, name);
            }
        };
    });

    /**
     * 初始化树组件面板
     * @param tree 树组件
     * @private
     */
    /**
     *
     * @param tree
     * @param admin
     * @param view
     * @private
     */
    function _initTreePanel(tree, admin, view,name) {
        if (name == undefined) {
            name = '';
        }
        var data = {
            name: name
        };
        admin.req({
            url: '/api/resources/tree',
            data: data,
            type: 'get',
            dataType: 'json',
            contentType: "application/json",
            success: function (res) {
                tree.render({
                    elem: "#resource_tree"
                    , data: res.data
                    , onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                    , click: function (obj) {
                        layer.msg(JSON.stringify(obj.data));
                    }
                });
            },
            error: function (res) {
                view.error('操作失败');
            }
        });
    }

    // 监听回车事件
    function listendown(e){
        if(!e) e = window.event;
        if(e.keyCode ===13){
            alert(555);
        }
    }
</script>